package lunadevs.luna.module.exploits;

import com.darkmagician6.eventapi.EventTarget;

import lunadevs.luna.category.Category;
import lunadevs.luna.events.EventPacket;
import lunadevs.luna.events.EventPlayerUpdate;
import lunadevs.luna.events.EventType;
import lunadevs.luna.module.Module;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.inventory.GuiChest;
import net.minecraft.inventory.Container;
import net.minecraft.network.play.server.S30PacketWindowItems;

public class ChestStealer extends Module{

	  public static S30PacketWindowItems packet;
	  public boolean shouldEmptyChest;
	  public static int delay;
	  public int currentSlot;
	  public static int[] whitelist;
	
	public ChestStealer() {
		super("ChestStealer", 0, Category.EXPLOITS, true);
	    this.delay = 0;
	    this.whitelist = new int[] { 54 };
	}

	  private int getNextSlot(Container container)
	  {
	    int i = 0;
	    for (int slotAmount = container.inventorySlots.size() == 90 ? 54 : 35; i < slotAmount; i++) {
	      if (container.getInventory().get(i) != null) {
	        return i;
	      }
	    }
	    return -1;
	  }
	  
	  public boolean isContainerEmpty(Container container)
	  {
	    boolean temp = true;
	    int i = 0;
	    for (int slotAmount = container.inventorySlots.size() == 90 ? 54 : 35; i < slotAmount; i++) {
	      if (container.getSlot(i).getHasStack()) {
	        temp = false;
	      }
	    }
	    return temp;
	  }
	  
	  @EventTarget
	  public void onUpdate(EventPlayerUpdate event)
	  {
		  if(!this.isEnabled) return;
	    if (event.getType() == EventType.PRE) {
	      try
	      {
	        if ((!mc.inGameHasFocus) && (this.packet != null) && (Minecraft.thePlayer.openContainer.windowId == this.packet.func_148911_c()) && ((mc.currentScreen instanceof GuiChest))) {
	          if (!isContainerEmpty(Minecraft.thePlayer.openContainer))
	          {
	            int slotId = getNextSlot(Minecraft.thePlayer.openContainer);
	            if (this.delay >= 1.01F)
	            {
	              mc.playerController.windowClick(Minecraft.thePlayer.openContainer.windowId, slotId, 0, 1, Minecraft.thePlayer);
	              this.delay = 0;
	            }
	            this.delay += 1.01F;
	          }
	          else
	          {
	            Minecraft.thePlayer.closeScreen();
	            z.addNiceMessage("Closing container");;
	            this.packet = null;
	          }
	        }
	      }
	      catch (Exception e)
	      {
	        e.printStackTrace();
	      }
	    }
	  }
	  
	  @EventTarget
	  public void onPacket(EventPacket event)
	  {
		  if(!this.isEnabled) return;
	    if ((event.getType() == EventPacket.EventPacketType.RECEIVE) && ((event.getPacket() instanceof S30PacketWindowItems))) {
	      this.packet = (S30PacketWindowItems)event.getPacket();
	    }
	  }
	
	@Override
	public String getValue() {
		return "Fast";
	}

}
